//Will Zepeda
//GSP-362
//PU_Rotate.cpp
//class used to execute Rotate Tile power-up
#include "PURotate.h"
#include "Controller.h"


PU_Rotate::PU_Rotate()
{
	mpController	= Controller::Instance();
	mpBoard			= mpController->getBoard();
	mpResetTile		= NULL;
	mTileSelected	= false;
	mExecuted		= false;
	PU_name			= "Rotate Power-Up";
}
void PU_Rotate::addLightingUI()
{
	XY temp;
	//change lighting UI
	for(int i=0;i<NUM_ROWS_AND_COLUMNS;++i)
	{
		for(int j=0;j<NUM_ROWS_AND_COLUMNS;++j)
		{
			temp.X = j; temp.Y = i;
			if(mpBoard->getTile(temp)->isMovable())
				mpBoard->getTile(temp)->setMatToGreen();
			else
				mpBoard->getTile(temp)->setMatToRed();
		}
	}
	mpBoard->getCurrentTile()->setMatToYellow();
}
void PU_Rotate::removeLightingUI()
{
	//change lighting UI
	XY temp;
	for(int i=0;i<NUM_ROWS_AND_COLUMNS;++i)
	{
		for(int j=0;j<NUM_ROWS_AND_COLUMNS;++j)
		{
			temp.X = j; temp.Y = i;
			mpBoard->getTile(temp)->setMatToWhite();
		}
	}
}
int PU_Rotate::Execute(Input* aInput, Player* aPlayers, Text * aText)
{
	//aText->setPosition(0, SCREEN_HEIGHT/10);	  
	aText->setText( "Use <SHIFT> keys to look at your current goal.\n"
					"Press <CTRL> for help.");
	//if this is the first time entering powerUp, in ones turn, set
	//current tile to middle tile.
	if(!mExecuted)
	{
		XY bdPos(3,3);
		mpBoard->setCurrentTile(bdPos);
		mpResetTile = mpBoard->getCurrentTile();
		mExecuted = true;
	}
	
	//changes materials based on tile's movable bool
	addLightingUI();

	//if a tile has not been selected for rotation
	//get input to move selector and switch which one is active tile
	if(!mTileSelected)
	{
		if(aInput->KeyDown(DIK_LCONTROL) || aInput->KeyDown(DIK_RCONTROL))
		{
			//set instructions text
			//aText->setPosition(0, SCREEN_HEIGHT/4);	
			aText->setText(	"* Choose a tile to rotate using the highlighted tile.\n"
							"* Red tiles cannot be rotated.\n"
							"* Use WASD to move the highlight tile.\n"
							"* Use <SPACE> bar to select the tile.");
		}

		//make selected tile a tile that is NORTH from this tile as long
		//as it is movable.
		if(aInput->KeyPressed(DIK_W))
		{
			//check to see what tile is NORTH, if any
			Tile * northTile = mpBoard->getN();
			//check to see if north tile is not NULL
			if(northTile)
			{
				if(northTile->isMovable())
				{
					mpBoard->setCurrentTile(northTile);
					mpResetTile = northTile;
				}
				else
				{
					do
					{
						mpBoard->setCurrentTile(northTile);
						northTile = mpBoard->getN();
						if(northTile && northTile->isMovable())
							mpResetTile = northTile;
					}while(northTile && !northTile->isMovable());
					//make the last "good" tile the curretTile
					mpBoard->setCurrentTile(mpResetTile);
				}
			}
		}
		//make selected tile a tile that is WEST from this tile && !m_movable
		if(aInput->KeyPressed(DIK_A))
		{
			//check to see what tile is WEST, if any
			Tile * westTile = mpBoard->getW();
			//check to see if west tile is not NULL
			if(westTile)
			{
				if(westTile->isMovable())
				{
					mpBoard->setCurrentTile(westTile);
					mpResetTile = westTile;
				}
				else
				{
					//keep checking until you come across first 
					//movable tile
					do
					{
						mpBoard->setCurrentTile(westTile);
						westTile = mpBoard->getW();
						if(westTile && westTile->isMovable())
							mpResetTile = westTile;
					}while(westTile && !westTile->isMovable());
					//make the last "good" tile the curretTile
					mpBoard->setCurrentTile(mpResetTile);
				}
			}
		}
		//make selected tile a tile that is SOUTH from this tile && !m_movable
		if(aInput->KeyPressed(DIK_S))
		{
			//check to see what tile is SOUTH, if any
			Tile * southTile = mpBoard->getS();
			//check to see if south tile is not NULL
			if(southTile)
			{
				if(southTile->isMovable())
				{
					mpBoard->setCurrentTile(southTile);
					mpResetTile = southTile;
				}
				else
				{
					do
					//keep checking until you come across first 
					//movable tile
					{
						mpBoard->setCurrentTile(southTile);
						southTile = mpBoard->getS();
						if(southTile && southTile->isMovable())
							mpResetTile = southTile;
					}while(southTile && !southTile->isMovable());
					//if true, make that tile the curretTile
					mpBoard->setCurrentTile(mpResetTile);
				}
			}
		}
		//make selected tile a tile that is EAST from this tile && !m_movable
		if(aInput->KeyPressed(DIK_D))
		{
			//check to see what tile is EAST, if any
			Tile * eastTile = mpBoard->getE();
			//check to see if east tile is not NULL
			if(eastTile)
			{
				if(eastTile->isMovable())
				{
					mpBoard->setCurrentTile(eastTile);
					mpResetTile = eastTile;
				}
				else
				{
					do
					//keep checking until you come across first 
					//movable tile
					{
						mpBoard->setCurrentTile(eastTile);
						eastTile = mpBoard->getE();
						if(eastTile && eastTile->isMovable())
							mpResetTile = eastTile;
					}while(eastTile && !eastTile->isMovable());
					//make that tile the curretTile
					mpBoard->setCurrentTile(mpResetTile);
				}
			}
		}
		//tells the power-up that you selected a tile to rotate
		if(aInput->KeyPressed(DIK_SPACE))
		{
			mTileSelected = true;
		}
	}
	else
	{
		if(aInput->KeyDown(DIK_LCONTROL) || aInput->KeyDown(DIK_RCONTROL))
		{
			//set instructions text
			//aText->setPosition(0, SCREEN_HEIGHT/4);	
			aText->setText( "* Use WASD to rotate the selected tile.\n"
							"* Use <SPACE> bar to set the orientation of the tile.");
		}

		//once tile was selected
		//rotate the selected tile with ARROW_UP & ARROW_DOWN
		if(aInput->KeyPressed(DIK_UP)    || aInput->KeyPressed(DIK_W))
			mpBoard->getCurrentTile()->setOrientation(NORTH);
		if(aInput->KeyPressed(DIK_DOWN)  || aInput->KeyPressed(DIK_S))
			mpBoard->getCurrentTile()->setOrientation(SOUTH);
		if(aInput->KeyPressed(DIK_LEFT)  || aInput->KeyPressed(DIK_A))
			mpBoard->getCurrentTile()->setOrientation(WEST);
		if(aInput->KeyPressed(DIK_RIGHT) || aInput->KeyPressed(DIK_D))
			mpBoard->getCurrentTile()->setOrientation(EAST);
		if(aInput->KeyPressed(DIK_SPACE))
		{
			//change executed back to false to reset for next time it'll get called
			mExecuted = false;
			//reset value for tile selected for next time
			mTileSelected = false;
			//change material back to white
			removeLightingUI();
			//return move to change game state
			return MOVE;
		}
	}
	//return STAY if all the steps of the power-up have not been completed
	//this ensures you keep coming back here to finished executing the power- up
	return STAY;
}